package com.zack.util.web.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpRequestUtil {
    // 这个list记录搜索无结果的query
    private static List<String> badQuerys = new ArrayList<>();
    public static StringBuilder wholeContent = new StringBuilder();

    /**
     * 传入一个url获取页面内容， 写入指定文件中.并返回页面内容。注意这里的请求是Get请求。
     * 通过字符长度确定是否超时， 如果连续30次超时就认为是搜索无结果。
     *
     * @param urlString
     * @return
     */
    public static void toQuery(String urlString, String query) {
        int maxReqTime = 70;
        StringBuilder toContentString = new StringBuilder();
        //创建一个response 对象用来接收response

        boolean timeOut = true;
        int cntRunNum = 0;
        // 创建一个httpClient对象
        CloseableHttpClient httpClient = null;
        try {
            while (timeOut) {
            HttpResponse response2 = null;
            HttpEntity entity2 = null;
            RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();//标准Cookie策略
            BasicCookieStore cookieStore = new BasicCookieStore();
            httpClient = HttpClients.custom()
                    .setDefaultCookieStore(cookieStore)
                    .setDefaultRequestConfig(requestConfig)
                    .setMaxConnPerRoute(20)
                    .build();
            HttpEntity entity;
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            //设置里面的参数
            params.add(new BasicNameValuePair("q", query));
            params.add(new BasicNameValuePair("page", "1"));
            params.add(new BasicNameValuePair("size", "50"));
//            params.add(new BasicNameValuePair("uid", "99976"));
            String str = "";
            params.add(new BasicNameValuePair("cityEname", "henan"));
            str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
            urlString = urlString + "?" + str;
            HttpGet get = new HttpGet(urlString);
            get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
            get.addHeader("Accept-Encoding", "gzip, deflate");
            get.addHeader("Connection", "close");

                System.out.println("正在跑: "+query);
                response2 = httpClient.execute(get);
                entity2 = response2.getEntity();
                try {
                    BufferedReader bf1 = new BufferedReader(new InputStreamReader(entity2.getContent()));
                    String line2 = "";
                    int responLength = 0;
                    StringBuilder cont = new StringBuilder();
                    while ((line2 = bf1.readLine()) != null) {
                        responLength+=line2.length();
                        cont.append(line2);
                    }
                    cntRunNum++;
                    System.out.println(responLength+"---"+cntRunNum);
                    if (responLength < 80 && cntRunNum < maxReqTime){
                        //这种情况当成超时处理。
                        System.out.println("超时继续： "+cntRunNum);
                        Thread.sleep(50);
                        continue;
                    }
                    if (responLength < 80 && cntRunNum >= maxReqTime){
                        System.out.println("未查到");
                        badQuerys.add(query);
                        wholeContent.append(query+"--@@@--"+"未找到"+"\n");
                        break;
                    }
                        System.out.println("query： "+query+"----："+responLength);
                        wholeContent.append(query+"--@@@--"+cont+"\n");
                        timeOut = false;
                        break;
                } catch (Exception e) {
                    System.out.println("Exception IOEception: " + e);
                }
            }
        } catch (Exception e) {
            System.out.println("Client error Exception: " + e);
        }finally{
            try {
                if (null != httpClient)
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
